﻿using Bear.BLL;
using Bear.Library.Util;
using Bear.Library.WinApi;
using Bear.Models;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Bear.BLL.MapleStory
{
    public class Login : ScriptBase
    {
        public override void Run(DataCenter data)
        {
            this.OnShowMessage("开始登录...");

            var rect = new Bear.Library.WinApi.Rect();
            this.OnShowMessage("输入账户名...");
            WinApiHelper.GetWindowRect(data.Handle, ref rect);


            int BorderWidth = (rect.Right - rect.Left - 800) / 2;
            int TitleHeight = rect.Bottom - rect.Top - 600 - BorderWidth;

            //鼠标移动到账户名
            int moveX = rect.Left + BorderWidth + 425;
            int moveY = rect.Top + TitleHeight + 295;

            KeyboardMouseOperator.MouseMove(new Point(moveX, moveY));
            Thread.Sleep(800);
            //双击
            KeyboardMouseOperator.MouseDoubleClick();
            Thread.Sleep(1000);
            //输入账户名
            KeyboardMouseOperator.InputString(data.CurrentPlayer.AccountName);
            Thread.Sleep(1000);

            this.OnShowMessage("输入密码...");
            //双击
            moveY = rect.Top + TitleHeight + 320;
            KeyboardMouseOperator.MouseMove(new Point(moveX, moveY));
            Thread.Sleep(800);
            KeyboardMouseOperator.MouseDoubleClick();
            Thread.Sleep(1000);
            KeyboardMouseOperator.InputString(data.CurrentPlayer.Password);
            Thread.Sleep(1000);

            this.OnShowMessage("登录游戏...");
            KeyboardMouseOperator.KeyPress(Keys.Enter);
            Thread.Sleep(5000);

            this.OnShowMessage("进入大区...");
            KeyboardMouseOperator.KeyPress(Keys.Enter);
            Thread.Sleep(3000);



            this.OnShowMessage("选择频道" + data.Config.Channel + "...");

            for (int i = 0; i < data.Config.Channel / 5; i++)
            {
                KeyboardMouseOperator.KeyPress(Keys.Down);
                Thread.Sleep(500);
            }

            for (int i = 0; i < data.Config.Channel % 5; i++)
            {
                KeyboardMouseOperator.KeyPress(Keys.Right);
                Thread.Sleep(500);
            }
            KeyboardMouseOperator.KeyPress(Keys.Enter);
            Thread.Sleep(2000);

            this.OnShowMessage("选择人物{0}...", data.CurrentPlayer.Role);

            //先恢复到首个人物

            for (int i = 0; i < 10; i++)
            {
                KeyboardMouseOperator.KeyPress(Keys.Left);
                Thread.Sleep(500);
            }


            for (int i = 0; i < data.CurrentPlayer.Role - 1; i++)
            {
                KeyboardMouseOperator.KeyPress(Keys.Right);
                Thread.Sleep(500);
            }

            KeyboardMouseOperator.KeyPress(Keys.Enter);
            Thread.Sleep(2000);

            this.OnShowMessage("获取安全码{0}位置...", data.CurrentPlayer.SecurityCode);

            List<Point> lstCode = new List<Point>();

            using (Bitmap bitmap = ScreenCapturer.Capture(data.Handle))
            {
                foreach (var item in data.CurrentPlayer.SecurityCode.ToCharArray())
                {
                    using (Image temp = Bitmap.FromFile(string.Format(@"Datas\SecurityCode\{0}.png", item)))
                    {
                        List<Point> lst = CvHelper.TemplateMatching(bitmap, (Bitmap)temp, 0.9);

                        lstCode.Add(lst.FirstOrDefault());
                    }
                }
            }


            this.OnShowMessage("输入安全码...");
            foreach (var item in lstCode)
            {

                int codeX = rect.Left + BorderWidth + item.X;
                int codeY = rect.Top + TitleHeight + item.Y;

                KeyboardMouseOperator.MouseMove(new Point(codeX, codeY));
                Thread.Sleep(500);
                //双击
                KeyboardMouseOperator.MouseClick(true);
                Thread.Sleep(800);
            }
            this.OnShowMessage("进入游戏...");
            KeyboardMouseOperator.KeyPress(Keys.Enter);
            Thread.Sleep(5000);

        }
    }
}
